package com.woniu.service;


import com.woniu.entity.Checkinfo;
import com.woniu.entity.Students;
import com.woniu.mapper.StudentsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 杨光 微信号:tolryg
 * @time 15:14
 */

@Service
public class StudentsService {



    @Autowired
    private StudentsMapper studentsMapper;


    public List<Students> findAllStudents(){
        return studentsMapper.selectAll();
    }

    //从文件导入学生
    public void addStudent(MultipartFile file) throws IOException {

        //导入学生的具体逻辑
        //读取文件的内容 （Alt + Enter）

        //把上传的文件保存到D:/study/checkin/students.txt
        file.transferTo(new File("D:/study/checkin/students.txt"));

        //把文件内容读出来，变为String的列表

        List<String> lines = Files.readAllLines(Paths.get("D:/study/checkin/students.txt"), StandardCharsets.UTF_8);

        for (String line : lines) {
            System.out.println(line);
            String[] split = line.split(",");
            Students s = new Students();
            s.setStuName(split[0]);
            s.setStuID(split[1]);
            s.setStuClass(split[2]);
            s.setStatus(split[3]);
            s.setCourses(split[4]);

            studentsMapper.insert(s);
        }

    }


    public boolean saveStudents(List<Students> list) {

        for (Students students : list) {
            studentsMapper.updateByPrimaryKeySelective(students);
            //update students set stuName='谈璐',stuID='WNSH202303013',stuClass='WNSHJ078',status='退学',courses='java' where id=2
        }

        return true;

    }


}
